﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Wmi
{
    public enum WmiNamespace
    {
        Dfsr,
        Cimv2,
        Microsoft,
        MicrosoftAD,
        MSCluster,
        Perfmon,
    }

    public class Connection
    {
        private string[] WmiNameSpacePath = { "\\root\\microsoftdfs", 
                                                "\\root\\CIMV2", 
                                                "\\root\\Microsoft", 
                                                "\\root\\MicrosoftActiveDirectory",
                                                "\\root\\MSCluster",
                                                "\\root\\perfmon" };


        private System.Management.ConnectionOptions connectionOptions = new System.Management.ConnectionOptions();
        private System.Management.ManagementScope managementScope;
        private System.Management.ManagementPath managementPath = new System.Management.ManagementPath();

        public System.Management.ManagementScope ManagementScope
        {
            get { return managementScope; }
        }
        public Connection(String Server, WmiNamespace wminspath)
        {
            try
            {
                managementPath.Path = WmiNameSpacePath[(int)wminspath];
                managementPath.Server = Server;
                managementScope = new System.Management.ManagementScope(managementPath);
            }
            catch (System.Runtime.InteropServices.COMException come)
            {
                string error = "Message: " + come.Message + " Source: " + come.Source;
                Console.Error.WriteLine(error);
                return;
            }
            catch (System.UnauthorizedAccessException uae)
            {
                string error = "Message: " + uae.Message + " Source: " + uae.Source;
                Console.Error.WriteLine(error);
                return;
            }
            catch (Exception e)
            {
                string error = "Message: " + e.Message + " Source: " + e.Source;
                Console.Error.WriteLine(error);
                return;
            }
        }
        public Connection(String Server, WmiNamespace wminspath, String Username, String Password)
        {
            try
            {
                managementPath.Path = WmiNameSpacePath[(int)wminspath];
                managementPath.Server = Server;

                connectionOptions.Username = Username;
                connectionOptions.Password = Password;
                managementScope = new System.Management.ManagementScope(managementPath, connectionOptions);
            }
            catch (System.Runtime.InteropServices.COMException come)
            {
                string error = "Message: " + come.Message + " Source: " + come.Source;
                Console.Error.WriteLine(error);
                return;
            }
            catch (System.UnauthorizedAccessException uae)
            {
                string error = "Message: " + uae.Message + " Source: " + uae.Source;
                Console.Error.WriteLine(error);
                return;
            }
            catch (Exception e)
            {
                string error = "Message: " + e.Message + " Source: " + e.Source;
                Console.Error.WriteLine(error);
                return;
            }
        }
    }
}
